package top.jlzhou.tools.http;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



/**
 * 需要发送的请求数据模型
 * @author J.L.Zhou
 *
 */
public class Request {

	/**
	 * 请求方式
	 * @author J.L.Zhou
	 *
	 */
	public enum Method {
		POST,GET,HEAD,DELETE,OPTIONS,PUT,TRACE,
	}
	
	/**
	 * 请求地址
	 */
	private String action;
	/**
	 * 请求方式
	 */
	private Method method = Method.POST;
	/**
	 * 请求参数
	 */
	private List<Parameter> parameter=new ArrayList<Parameter>(0);
	/**
	 * 请求上传文件
	 */
	private List<MultipartFile> files=new ArrayList<MultipartFile>(0);
	/**
	 * 请求头
	 */
	private List<Header> headers=new ArrayList<Header>(0);
	

	public Request(String action) {
		super();
		this.action = action;
	}
	public Request() {
		super();
	}
	
	
	public Method getMethod() {
		return method;
	}
	public Request setMethod(Method method) {
		this.method = method;
		return this;
	}
	
	public String getAction() {
		return action;
	}
	public Request setAction(String action) {
		this.action = action;
		return this;
	}
	
	public List<Parameter> getParameters() {
		return parameter;
	}

	public List<MultipartFile> getFiles() {
		return files;
	}

	
	
	
	public List<Header> getHeaders() {
		return headers;
	}

	
	/**
	 * 添加请求参数
	 * @param name
	 * @param value
	 * @return
	 */
	public Request addParameter(String name,String value){
		parameter.add(new Parameter(name, value));
		return this;
	}
	
	/**
	 * 设置请求参数，相同名称的第一个参数会覆盖，否则添加
	 * @param name
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unused")
	public Request setParameter(String name,String value){
		if(name==null||"".equals(name)){
			throw new RuntimeException("参数名不能为空!");
		}
		for(int i=0;i<parameter.size();i++){
			if(parameter.get(i).getName().equals(name)){
				parameter.get(i).setValue(value==null?"":null);
			}
			return this;
		}
		parameter.add(new Parameter(name, value));
		return this;
	}
	
	/**
	 * 添加上传文件，文件必须存在
	 * @param name
	 * @param value
	 * @return
	 */
	public Request addFile(String name,File value){
		files.add(new MultipartFile(name, value));
		return this;
	}
	
	/**
	 * 添加请求头
	 * @param name
	 * @param value
	 * @return
	 */
	public Request addHeader(String name,String value){
		headers.add(new Header(name, value));
		return this;
	}
	
	/**
	 * 设置请求头，相同名称的第一个请求头会覆盖，否则添加
	 * @param name
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unused")
	public Request setHeader(String name,String value){
		if(name==null||"".equals(name)){
			throw new RuntimeException("请求头名不能为空!");
		}
		for(int i=0;i<headers.size();i++){
			if(headers.get(i).getName().equals(name)){
				headers.get(i).setValue(value==null?"":null);
			}
			return this;
		}
		headers.add(new Header(name, value));
		return this;
	}
	
	public Request clearHeader(){
		headers.clear();
		return this;
	}
	
	public Request clearParameter(){
		parameter.clear();
		return this;
	}
	
	public Request clearFile(){
		files.clear();
		return this;
	}
	
	
	public Request simulationChrome(){
		addHeader("Connection", "keep-alive");
		addHeader("Pragma", "no-cache");
		addHeader("Cache-Control", "no-cache");
		addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36");
		addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		addHeader("Accept-Encoding", "gzip, deflate, br");
		addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		return this;
	}
	
}
